package com.example.testapp.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.activity.study.PhonePhotoDetailActivity;
import com.example.testapp.activity.http.AsyncHttpUtil;
import com.example.testapp.activity.http.Contacts;
import com.example.testapp.activity.model.PhonePaging;
import com.example.testapp.activity.model.PhonePhotoItem;
import com.example.testapp.activity.ui.DataTask;
import com.example.testapp.activity.util.JsonUtils;
import com.example.testapp.activity.util.MD5Utils;
import com.example.testapp.activity.view.ErrorHintView;
import com.example.testapp.activity.view.listview.PullToRefreshAdapterViewBase;
import com.example.testapp.activity.view.listview.PullToRefreshBase;
import com.example.testapp.activity.view.listview.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * 科技资讯
 * Created by l on 2016/8/25.
 */
public class PhoneGrilFragment extends BaseFragment {

    private View view;
    private PullToRefreshListView  mListView;
    private ErrorHintView   mErrorHintView;
    private List<PhonePhotoItem> photoList = new ArrayList<>();
    private PhonePhotoAdapter   mAdapter;

    private int offset = 0;				//第N条数据
    private boolean isPage = true;		//是否还有下一页
    private LinearLayout footerView;    //最后一条listview

    public static int VIEW_LIST = 1;
    /**显示断网**/
    public static int VIEW_WIFIFAILUER = 2;
    /** 显示加载数据失败 **/
    public static int VIEW_LOADFAILURE = 3;
    public static int VIEW_LOADING = 4;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_gril, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mListView = (PullToRefreshListView)view.findViewById(R.id.phone_photo_list);
        mErrorHintView = (ErrorHintView)view.findViewById(R.id.hintView);
        mAdapter = new PhonePhotoAdapter();
        mListView.setAdapter(mAdapter);
        setupIdleListener();
        showLoading(VIEW_LOADING);
        refreshData(true);
    }

    private void setupIdleListener() {
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                 if(refreshView.isHeaderShown()){
                     defParams();
                     refreshData(true);
                 }
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PhonePhotoItem item = photoList.get(position - 1);
                Intent intent = new Intent(mContext, PhonePhotoDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("PhonePhotoItem", item);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });

        mListView.setOnPreRefreshingAnimListener(new PullToRefreshAdapterViewBase.OnPreRefreshingAnimListener() {
            @Override
            public void onPreRefreshingAnim() {
                 mListView.setFooterLoadingViewHeaderText("加载更多图片");
            }
        });

        mListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                if (isPage){
                    refreshData(false);  //滑动到底部自动刷新
                }else{
                    //没有下一页了
                    mListView.onRefreshComplete();
                    if(footerView == null){
                        footerView = (LinearLayout)View.inflate(mContext, R.layout.item_pull_to_refresh_footer, null);
                        TextView   footer = (TextView)footerView.findViewById(R.id.footer);
                        footer.setText("到底了呢-共" + photoList.size() + "条资讯");
                        mListView.getRefreshableView().addFooterView(footerView);
                    }
                }
            }
        });
    }

    private void showLoading(int i) {
        mListView.setVisibility(View.GONE);
        mErrorHintView.setVisibility(View.GONE);

        switch (i){
            case 1:
                mErrorHintView.hideLoading();
                mListView.setVisibility(View.VISIBLE);
                break;
            case 2:
                mErrorHintView.hideLoading();
                mErrorHintView.netError(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        defParams();
                        showLoading(VIEW_LOADING);
                        refreshData(true);
                    }
                });
                break;
            case 3:
                mErrorHintView.hideLoading();
                mErrorHintView.loadFailure(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        defParams();
                        showLoading(VIEW_LOADING);
                        refreshData(true);
                    }
                });
                break;
            case 4:
                mErrorHintView.loadingdata();
                break;
        }
    }

    /**
     * 恢复默认参数
     */
    private void defParams() {
       offset = 0;
       isPage = false;
    }

    private void refreshData(boolean clean) {
        dataTask = new RefreshDataTask(dataTask.getID() + 1, clean);
        dataTask.run();
    }

    public class RefreshDataTask extends DataTask{
        boolean clean = false;		// 是否清空原数据

        public RefreshDataTask(int id, boolean clean) {
            super(id);
            this.clean = clean;
        }

        @Override
        public void run() {
            if(getID() < dataTask.getID()){
                return;
            }else{
                loadPhonePhotoInfo(clean);
            }
        }
    }

    /**
     * 加载最新的手机资讯图片
     * @param clean
     */
    private void loadPhonePhotoInfo(final boolean clean) {
        RequestParams params = new RequestParams();
        String  t = String.valueOf(System.currentTimeMillis() / 1000);  //获取当前时间
        params.put("timestamp", t);  //当前时间戳
        final String token1 = MD5Utils.md5("d19cf361181f5a169c107872e1f5b722" + t);
        params.put("token1", token1);

        params.put("apiid", 3);
        params.put("module", "api_libraries_sjdbg_aimeizhi");
        params.put("returnformat", "json");
        params.put("encoding", "utf-8");
 //		params.put("onetime", timeStamp);			//分页用，传第一条新闻的时间戳，第一页下不用传值。
        params.put("offset", offset);
        params.put("rows", 10);

        AsyncHttpUtil.get(Contacts.PHONE_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                 if(footerView != null){
                     mListView.getRefreshableView().removeFooterView(footerView);
                     footerView = null;
                 }

                mListView.onRefreshComplete();

                if(responseBody != null && responseBody.length > 0){
                    try {
                        JSONObject object = new JSONObject(new String(responseBody));
                        String status = object.getString("status");
                        if (TextUtils.equals("1", status)){
                            offset += 10;
                            JSONObject data = object.getJSONObject("data");
                            JSONArray result = data.getJSONArray("result");
                            JSONObject paging = data.getJSONObject("paging");
                            if (result != null){
                                List<PhonePhotoItem> list = JsonUtils.getInstance(PhonePhotoItem.class, result);
                                if (list != null && list.size() > 0){
                                    if (clean){
                                        photoList.clear();
                                    }
                                    photoList.addAll(list);
                                    showLoading(VIEW_LIST);
                                    mAdapter.notifyDataSetChanged();
                                }else{
                                    showLoading(VIEW_LOADFAILURE);
                                }
                            }else{
                                showLoading(VIEW_LOADFAILURE);
                            }
                            if (paging != null){
                                PhonePaging instance = JsonUtils.getInstance(PhonePaging.class, paging);
                                if (instance != null){
                                    int total = Integer.parseInt(instance.getTotal());
                                    if (offset >= total){
                                        isPage = false;
                                    }else{
                                        isPage = true;
                                    }
                                }
                            }
                        }
                    }catch (Exception e){
                        showLoading(VIEW_LOADFAILURE);
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                 showLoading(VIEW_WIFIFAILUER);
            }
        });
    }

    private class PhonePhotoAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return photoList.size();
        }

        @Override
        public Object getItem(int position) {
            return photoList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null){
                holder = new ViewHolder();
                convertView = View.inflate(mContext, R.layout.view_item_phone_photo_lv, null);
                holder.img = (ImageView)convertView.findViewById(R.id.img);
                holder.text = (TextView)convertView.findViewById(R.id.text);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            PhonePhotoItem item = photoList.get(position);
            holder.item = item;

            if (!TextUtils.isEmpty(item.getPic())){
                Picasso.with(mContext).load(item.getPic()).placeholder(R.color.ECECEC).error(R.color.ECECEC).into(holder.img);
            }

            if (!TextUtils.isEmpty(item.getTitle())){
                holder.text.setText(item.getTitle());
            }else{
                holder.text.setText("");
            }

            return convertView;
        }

        public class ViewHolder{
            ImageView img;
            TextView  text;
            PhonePhotoItem item;
        }
    }
}
